package yl.hs.bmipfull.controllers.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.system.RoleInfoEntity;
import yl.hs.bmipfull.service.system.IMenuInfoService;
import yl.hs.bmipfull.service.system.IRoleService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RoleController 角色信息操作web接口
 *
 * @author 谢景广
 * @category 系统管理——角色管理
 */
@Api(value = "角色信息Web接口", tags = "系统管理——角色管理")
@RequestMapping(path = "/api/system")
@RestController
public class RoleController extends ControllerBase {

    @Autowired
    IRoleService roleSvc;
    @Autowired
    IMenuInfoService menuSvc;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询所有角色", notes = "查询角色信息")
    @RequestMapping(path = "/role/{rowNum}/{page}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult queryAll(@RequestBody PageQuery query, @PathVariable int page, @PathVariable int rowNum) {
        try {
            var result = roleSvc.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "保存角色信息", notes = "保存编辑或者新建的角色信息")
    @RequestMapping(path = "/role", method = RequestMethod.POST)
    @ResponseBody
    public WebResult save(@RequestBody SubmitModel model) {
        try {
            var result = roleSvc.saveRoleInfo(model.getData(), model.getSn());
            if (result != null && result > 0) {
                return success("数据已保存");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询角色信息", notes = "查询根据角色编号获取角色信息")
    @RequestMapping(path = "/role/{sn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult find(@ApiParam(value = "数据编号", required = true) @PathVariable("sn") String sn) {
        try {
            var role = roleSvc.findRoleBySn(sn);
            if (role != null) {
                return success("ok", role);
            } else {
                return notfound(String.format("没有找到编号为%s的角色信息", sn));
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询角色信息", notes = "根据角色名称关键词查询色信息")
    @RequestMapping(value = "/queryRole", method = RequestMethod.GET)
    public WebResult querySelect(@RequestParam(value = "key", required = false) String key) {
        List<RoleInfoEntity> result = roleSvc.queryAll();
        return success("ok", result);
    }

    @ApiOperation(value = "查询角色授权", notes = "根据角色编号获取角色授权")
    @RequestMapping(value = "/rolePermission/{sn}", method = RequestMethod.GET)
    public WebResult queryRolePermission(@PathVariable String sn) {
        var permissions = roleSvc.queryRolePermission(sn);
        var map = new HashMap<String, Object>();
        var tree = menuSvc.queryPermissionTree();
        map.put("menuTree", tree);
        map.put("permissions", permissions);
        return success("ok", map);
    }

    @ApiOperation(value = "保存角色授权", notes = "保存角色授权")
    @RequestMapping(value = "/rolePermission/", method = RequestMethod.POST)
    public WebResult saveRolePermission(@RequestBody Map<String, Object> form) {
        Integer result = roleSvc.saveRolePermission(form.get("roleSN").toString(), (List<String>) form.get("items"));
        return success("ok", result);
    }

    @ApiOperation(value = "删除角色信息", notes = "根据角色编号删除角色信息")
    @RequestMapping(path = "/role/{sn}", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@PathVariable("sn") String sn) {
        try {
            var result = roleSvc.deleteRoleBySN(sn.split(","));
            if (result > 0) {
                return success("数据已删除");
            } else
                return fail("没有删除任何信息");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}
